import 'package:ad_x_sdk_example/com/qlslylq/flutterdesign/constant/KeyConstant.dart';
import 'package:flutter/material.dart';
import 'package:ad_x_sdk_example/com/qlslylq/flutterdesign/scene/BaseScene.dart';
import 'package:flutter/services.dart';
import '../util/Log.dart';
import 'HomeScene.dart';

/*
 * 开屏广告测试页 <br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class SplashAdTestScene extends BaseScene {
  final bool isHalf;

  SplashAdTestScene({this.isHalf}) {}

  State<StatefulWidget> createState() {
    return new SplashAdTestSceneState();
  }
}

/*
 * 页面功能 <br/>
 */
class SplashAdTestSceneState extends BaseSceneState<SplashAdTestScene>
    with WidgetsBindingObserver {
  EventChannel eventChannel = EventChannel('com.qlslylq.ad-x-sdk/adListener');

  bool canJump = true;

  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    eventChannel.receiveBroadcastStream().listen((dynamic event) {
      String eventName = event['eventName'];
      if (eventName == "onAdShow") {
        Log.dt("onAdShow");
      } else if (eventName == "onAdShowError") {
        Log.dt("onAdShowError");
        routeToHomeScene();
      } else if (eventName == "onAdDismiss") {
        Log.dt("onAdDismiss");
        routeToHomeScene();
      } else if (eventName == "onAdClick") {
        Log.dt("onAdClick");
        showToast("点击了广告~");
      }
    }, onError: (dynamic error) {}, cancelOnError: true);
  }

  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.resumed:
        routeToHomeScene();
        canJump = true;
        break;
      case AppLifecycleState.paused:
        canJump = false;
        break;
    }
  }

  /*
   * 跳转主页<br/>
   */
  void routeToHomeScene() {
    if (!mounted) {
      return;
    }
    if (canJump) {
      canJump = false;
      switchingScene(new HomeScene(), finish: true);
    }
  }

  Widget build(BuildContext context) {
    num ratio = widget.isHalf ? 0.9 : 1.0;
    num adContainerHeightDp = MediaQuery.of(context).size.height * ratio;
    return buildBody(
        context,
        new Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Container(
                  width: double.infinity,
                  height: adContainerHeightDp,
                  child: new AndroidView(
                    viewType: "com.qlslylq.ad-x-sdk/splashAdView",
                    creationParams: {
                      "JH_SPLASH_POS_ID": KeyConstant.JH_SPLASH_POS_ID,
                      "w": 0,
                      "h": 0,
                      "fetchDelay": 5 * 1000,
                    },
                    creationParamsCodec: const StandardMessageCodec(),
                    onPlatformViewCreated: (int id) async {
                      var channel = new MethodChannel(
                          "com.qlslylq.ad-x-sdk/splashAdView_$id");
                      await channel.invokeMethod("initLoader");
                      await channel.invokeMethod("loadAndShow");
                    },
                  )),
              new Expanded(
                  child: new Container(
                alignment: Alignment.center,
                child: new Text('技术支持：https://gitee.com/qlslylq'),
              )),
            ]),
        showAppBar: false);
  }

  void onClick(Widget widget) {}
}
